package com.syh.jn.scm.service.impl.upstream.finance;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.event.annotation.DomainEventHandle;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.upstream.entity.finance.UpIncomeBill;
import com.syh.jn.scm.domain.upstream.entity.finance.UpStatementAccountBill;
import com.syh.jn.scm.domain.upstream.event.UpIncomeBillEvent;
import com.syh.jn.scm.domain.upstream.event.UpStatementAccountBillEvent;
import com.syh.jn.scm.domain.upstream.repository.finance.UpIncomeBillRepository;
import com.syh.jn.scm.dto.support.FilesDto;
import com.syh.jn.scm.dto.upstream.finance.UpIncomeBillVo;
import com.syh.jn.scm.service.api.upstream.finance.UpIncomeBillService;
import com.syh.jn.scm.service.api.upstream.finance.enums.UpIncomeBillEnum;
import com.syh.jn.scm.service.api.upstream.finance.enums.UpStatementAccountBillEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UpIncomeBillServiceImpl extends AbstractServiceImpl<Object, Object, UpIncomeBillVo, UpIncomeBill, Long> implements UpIncomeBillService {

  @Autowired
  private UpIncomeBillRepository upIncomeBillRepositoryImpl;

  @Override
  protected AggregateRepository<UpIncomeBill, Long> getAggregateRepository() {
    return upIncomeBillRepositoryImpl;
  }

  /**
   * 上传单据图片
   *
   * @param dto
   */
  @Override
  public void upload(FilesDto dto) {
    UpIncomeBill bill = upIncomeBillRepositoryImpl.getById(dto.getId());
    bill.upload(dto);
    upIncomeBillRepositoryImpl.upload(bill);
  }

  /**
   * 确认生效
   *
   * @param id
   * @return
   */
  @Override
  public Boolean confirmState(Long id) {
    UpIncomeBill bill = upIncomeBillRepositoryImpl.getById(id);
    bill.confirmState();
    upIncomeBillRepositoryImpl.update(bill);

    //双方确认(意思就是双方确认之后才纳入应收付记录和汇总)
    if (bill.getState().equals(UpIncomeBillEnum.CONFIRM.getStatus()) && bill.getConfirmState().equals(UpIncomeBillEnum.OTHERPARTY_CONFIRM.getStatus())) {
      Object[] obj = new Object[]{bill};
      //发送事件消息
      this.publicEvent(UpIncomeBillEvent.buildUpIncomeBillStateConfirmEvent(obj));
    }
    return Boolean.TRUE;
  }

  /**
   * 取消确认生效
   *
   * @param id
   * @return
   */
  @Override
  public Boolean cancelConfirm(Long id) {
    UpIncomeBill bill = upIncomeBillRepositoryImpl.getById(id);
    bill.cancelConfirm();
    upIncomeBillRepositoryImpl.update(bill);

    if (bill.getState().equals(UpIncomeBillEnum.CONFIRM.getStatus())) {
      Object[] obj = new Object[]{bill};
      //发送事件消息
      this.publicEvent(UpIncomeBillEvent.buildUpIncomeBillStateUnConfirmEvent(obj));
    }
    return Boolean.TRUE;
  }

  /**
   * 消费者：监听对账单（生成对账单状态和对账单号）
   *
   * @param bill
   */
  @DomainEventHandle(UpStatementAccountBillEvent.UP_STATEMENT_BILL_ACCOUNT_CREATED)
  protected void handleUpStatementBillAccountCreated(UpStatementAccountBill bill) {
    bill.getUpStatementAccountBillItems().forEach(billItem -> {
      if (billItem.getSourceType().equals(UpStatementAccountBillEnum.SOURCE_INCOME_BILL.getStatus())) {
        UpIncomeBill incomeBill = upIncomeBillRepositoryImpl.getById(billItem.getSourceId());
        incomeBill.generating(bill.getId());
        upIncomeBillRepositoryImpl.upload(incomeBill);
      }
    });
  }

  /**
   * 消费者：监听对账单（生成对账单状态和对账单号）
   *
   * @param bill
   */
  @DomainEventHandle(UpStatementAccountBillEvent.UP_STATEMENT_BILL_ACCOUNT_UPDATA)
  protected void handleUpStatementBillAccountUpdata(UpStatementAccountBill bill) {
    Wrapper wrapper = Wrapper.build();
    wrapper.append(Restrictions.eq("statementOfAccountId",bill.getId()));
    List<UpIncomeBill> upIncomeBills = upIncomeBillRepositoryImpl.getList(wrapper);
    upIncomeBills.forEach(upIncomeBill -> {
      upIncomeBill.cancelGenerating();
      upIncomeBillRepositoryImpl.upload(upIncomeBill);
    });
    bill.getUpStatementAccountBillItems().forEach(billItem -> {
      if (billItem.getSourceType().equals(UpStatementAccountBillEnum.SOURCE_INCOME_BILL.getStatus())) {
        UpIncomeBill incomeBill = upIncomeBillRepositoryImpl.getById(billItem.getSourceId());
        incomeBill.generating(bill.getId());
        upIncomeBillRepositoryImpl.upload(incomeBill);
      }
    });
  }

  /**
   * 消费者：监听对账单（删除对账单状态和对账单号）
   *
   * @param bill
   */
  @DomainEventHandle(UpStatementAccountBillEvent.UP_STATEMENT_BILL_ACCOUNT_DESTORY)
  protected void handleUpStatementBillAccountDestory(UpStatementAccountBill bill) {
    bill.getUpStatementAccountBillItems().forEach(billItem -> {
      if (billItem.getSourceType().equals(UpStatementAccountBillEnum.SOURCE_INCOME_BILL.getStatus())) {
        UpIncomeBill incomeBill = upIncomeBillRepositoryImpl.getById(billItem.getSourceId());
        incomeBill.cancelGenerating();
        upIncomeBillRepositoryImpl.upload(incomeBill);
      }
    });
  }
}
